import $ from 'jquery';
import './less/index.less';
import {request,GetQueryString} from "./utils/request";
import {GetAreaCode,GetScenicAreas} from "./utils/apis";
import Attractions from './image/Attractions.png';
//import './getCode.js';////董文强修改
// console.log(GetQueryString('AreaName'));
let myPoint = {},
    AreaName = GetQueryString('AreaName')? GetQueryString('AreaName'):"",
    AreaCode = "",
    AreaDataList = [],
    countI = 0,
    pageNo = 0;
var convertor;
var map = new BMap.Map("nomap");    // 创建Map实例
var geolocation = new BMap.Geolocation();
var geoc = new BMap.Geocoder();
//var geolocation = new BMap.Geolocation();
$(function () {
	setTimeout(function () {

	convertor = new BMap.Convertor();
    myFun();
    if(GetQueryString('AreaName')){
         //console.log('onload')
         //getCode(AreaName);
     }
    //获取城市code
    function getCityCode (AName){
        $(".index .head .site").html(AName)
        var data = {
            "AreaName":AName
        };
        request(GetAreaCode,data,(result)=>{
            AreaCode = (result[0]||{AreaCode:''}).AreaCode;
            AreaCode&&getAreaDataList(AreaCode);
        },()=>{
        		alert("code获取失败");
        },"post",false)
    
    }
    //获取景点列表
    function getAreaDataList (ACode){
        var data = {
            "AreaCode":ACode
        };
        request(GetScenicAreas,data,(result)=>{
            if(pageNo==0){
                AreaDataList = result;
                // console.log(AreaDataList)
            }else{
                // console.log("pageNo",pageNo)
                // AreaDataList = AreaDataList.concat(result);
            }
            // console.log("AreaCode",ACode)
            countI =0;
            editDom();

        },()=>{alert("景点列表获取失败");},"post")
    
    }
    
    function editDom(){
        AreaDataList.map((item2,index)=>{
            let Point = new BMap.Point(item2.ALNG,item2.ALAT);
            setTimeout(function () {
                convertor.translate([Point], 1, 5, function (data) {
                    if (data.status === 0) {
                        data.points.map(function (item, index2) {
                            var km = (Number((map.getDistance(myPoint, item)) || 0)/1000).toFixed(2);
                            AreaDataList[index]['km']=km;
                            AreaDataList[index]['points']=item;
                            countI ++ ;
                            if(countI == AreaDataList.length){
                                setDom()
                            }
                        })
                    }
                });
            }, 1);
        })
    }
    function setDom(){
        $(".index .content .scenicArea .list").html('');
        // console.log('AreaDataList',AreaDataList)
        var divItems = AreaDataList.map((item,index)=>{
            var km = item.km;
            var divItem = $(`<div class="item" >
                <div class="scenic-img">
                    <img src="${item.APIC}" alt="">
                </div>
                <div class="scenic-info">
                    <div class="scenic-name">${item.ANAME}${item.Price == 0?"<span class='mianfei'>免费</span>":""}</div>
                    <div class="scenic-introduction">${item.AINTRODUCTION}</div>
                    <div class="scenic-site"><i class="icon-site"></i> ${km}km</div>
                </div>
            </div>`);
            var img = divItem.find('img').get(0)
            img.addEventListener('error',function(){
                this.src = Attractions;
            })
            divItem.click(function(){
                localStorage.setItem('AMUSTPLAYLIST',item.AMUSTPLAYLIST);
                localStorage.setItem('Attractions',JSON.stringify(item));
                location.href=`map.html?AID=${item.AID}&ScenicName=${item.ANAME}&AttractionsPrice=${item.Price}`;
            })
            return divItem;
        }).sort((a,b)=>{
            var aKM = a.find('.scenic-site').text().replace('km','');
            var bKM = b.find('.scenic-site').text().replace('km','');
            if(Number(aKM)>Number(bKM))return 1;
            return -1;
        })
        $(".index .content .scenicArea .list").append(divItems);
    }
    //百度地图获取城市
    function myFun(result){
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){                
                myPoint = r.point;
                // myPoint = new BMap.Point(117.153722,39.090839);
                // map.centerAndZoom(myPoint, 11);

                geoc.getLocation(r.point, function(rs){
                    var addComp = rs.addressComponents;
                    if(AreaName.length>0){
                        AreaName&&getCityCode(AreaName);
                    }else{
                        AreaName = addComp.city;
                        getCityCode(AreaName);
                    }
                    // console.log('城市：',AreaName)
                });   
            }
            else {//董文强修改
              	AreaName = "北京市";
                getCityCode(AreaName);
                alert('failed'+this.getStatus());
            }        
        },{enableHighAccuracy: true})
		
    }
    $(".index div.head i,.index div.head span").click( function (e) {
        e.stopPropagation();
        //  $(this).hide();
        window.location.href="siteList.html?city="+AreaName;
    });
    $(".index div.downloadAPP").click((e)=>{
        // alert("打开APP")
        window.location.href = "com.example.mytu2://";
        window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.example.mytu2";
    })
    $(".index div.downloadAPP a.del").click((e)=>{
        e.stopPropagation();
        $(e.target).parent().hide();
    })
    
    }, 3000);

})